<?php
/**
 * 
 * 男朋友 处理类
 * @author tanzhe
 *
 */
class BoyfriendModel {
	private $db;
	private $cache;
	private $kissCd;
	private $chatCd;
	private $boyFriendDate;
	 
	public function __construct() {
		$this->db = new Data();
		$this->cache = new Cache();
		$this->kissCd = 5400;
		$this->chatCd = 1800;
		global $boyFriendDate;
		$this->boyFriendDate = $boyFriendDate;
	}
	
	/**
	 * 
	 * 获取男朋友相关信息
	 * @param str $key
	 * @param int $cache 0 不判断缓存  1、从缓存取
	 */
	public function get($key, $cache=1) { 
	 	$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( empty($playerInfo['boyfriend_id']) ) {
			return array('code'=>223,'ret'=>'');
		}
		//男朋友信息
		$boyfriendId = $playerInfo['boyfriend_id'];
		$boyfriendInfo = $cache == 1 ? $this->cache->get("boyfriend_".$boyfriendId) : array();
		if ( empty($boyfriendInfo) ) {
			$query = $this->db->query("select * from player_boyfriend where `id`={$playerInfo['boyfriend_id']}");
			$boyfriendInfo = $this->db->fetch_array($query);
		}
		$boyfriendInfo['clothes'] = empty($boyfriendInfo['clothes']) ? array() : $boyfriendInfo['clothes'];
		//print_r($boyfriendInfo);
		$time = time();
		$moodCd = 10;
		$reduceMood = floor(($time - $boyfriendInfo['update_time'])/$moodCd);
		if ( $reduceMood > 0 ) { //减少亲密度
			$updateData = array();
			$boyfriendInfo['mood'] -= $reduceMood;
			$boyfriendInfo['mood'] = $boyfriendInfo['mood'] < 0 ? 0 : $boyfriendInfo['mood'];
			if( $boyfriendInfo['mood'] == 0 ){ //已经过期 是挽留还是解雇
				return array('code'=>0,'ret'=>array('exceed'=>1));
			}
			$updateData['mood'] = $boyfriendInfo['mood'];
			$updateData['update_time'] = $boyfriendInfo['update_time'] + $reduceMood * $moodCd;
			$boyfriendInfo['update_time'] = $updateData['update_time'];
			//更新信息
			$options = array(
				'key'=>array(
					'id'=>$boyfriendId,
				),
				'data'=>$updateData,
			);
			$this->db->updateData('player_boyfriend', $options);
		}
		$this->cache->set("boyfriend_".$boyfriendId, $boyfriendInfo);
		$boyfriendInfo = $this->cache->get("boyfriend_".$boyfriendId);
		$ret = array(
			'name'=>$boyfriendInfo['name'],
			'job'=>$boyfriendInfo['job'],
			'manliness'=>$boyfriendInfo['manliness'],
			'dated'=>$time - $boyfriendInfo['add_time'],
			'favor'=>$boyfriendInfo['favor'],
			'mood'=>$boyfriendInfo['mood'],
			'kiss_cd'=>($boyfriendInfo['kiss_time'] + $this->kissCd) - $time,
			'chat_cd'=>($boyfriendInfo['chat_time'] + $this->chatCd) - $time,
			'bonus'=>unserialize($boyfriendInfo['bonus']),
			'clothes'=>$boyfriendInfo['clothes'],
		);
		return array('code'=>0,'ret'=>$ret);
	}

	/**
	 * 
	 * 亲吻
	 * @param str $key
	 */
	public function kiss($key) { 
	 	$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( empty($playerInfo['boyfriend_id']) ) {
			return array('code'=>223,'ret'=>'');
		}
		//男朋友信息
		$boyfriendId = $playerInfo['boyfriend_id'];
		$boyfriendInfo = $this->cache->get("boyfriend_".$boyfriendId);
		if ( empty($boyfriendInfo) ) {
			return array('code'=>2,'ret'=>'');
		}
		//cd时间未到
		$time = time();
		if ( (($boyfriendInfo['kiss_time'] + $this->kissCd) - $time ) > 0) {
			return array('code'=>226,'ret'=>'');
		}
		$updateData = array();
		$boyfriendInfo['favor'] += 1; //增加一点好感度
		$isGetItem = $this->getGiftbyFavor($boyfriendInfo['favor'], 1,$playerInfo);
		
		$updateData['favor'] = $boyfriendInfo['favor'];
		$updateData['kiss_time'] = $time;
		$boyfriendInfo['kiss_time'] = $updateData['kiss_time'];
		//更新信息
		$options = array(
			'key'=>array(
				'id'=>$boyfriendId,
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_boyfriend', $options);
		$this->cache->set("boyfriend_".$boyfriendId, $boyfriendInfo);
		$ret = array(
			'favor'=>$boyfriendInfo['favor'],
			'kiss_cd'=>($boyfriendInfo['kiss_time'] + $this->kissCd) - $time,
			'gift'=>$isGetItem===false ? '' : $isGetItem,
			'get_favor'=>1,
		);
		return array('code'=>0,'ret'=>$ret);
	}
	
	/**
	 * 
	 * 交谈
	 * @param str $key
	 */
	public function chat($key) { 
	 	$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( empty($playerInfo['boyfriend_id']) ) {
			return array('code'=>223,'ret'=>'');
		}
		//男朋友信息
		$boyfriendId = $playerInfo['boyfriend_id'];
		$boyfriendInfo = $this->cache->get("boyfriend_".$boyfriendId);
		if ( empty($boyfriendInfo) ) {
			return array('code'=>2,'ret'=>'');
		}
		//cd时间未到
		$time = time();
		if ( (($boyfriendInfo['chat_time'] + $this->chatCd) - $time ) > 0) {
			return array('code'=>227,'ret'=>'');
		}
		$updateData = array();
		$boyfriendInfo['mood'] += 5; //增加5点好感度
		$boyfriendInfo['mood'] = $boyfriendInfo['mood'] > 100 ? 100 : $boyfriendInfo['mood'];
		$updateData['mood'] = $boyfriendInfo['mood'];
		$updateData['chat_time'] = $time;
		$boyfriendInfo['chat_time'] = $updateData['chat_time'];
		//更新信息
		$options = array(
			'key'=>array(
				'id'=>$boyfriendId,
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_boyfriend', $options);
		$this->cache->set("boyfriend_".$boyfriendId, $boyfriendInfo);
		$ret = array(
			'mood'=>$boyfriendInfo['mood'],
			'chat_cd'=>($boyfriendInfo['chat_time'] + $this->chatCd) - $time,
			'get_mood'=>5,
		);
		return array('code'=>0,'ret'=>$ret);
	}
	
	/**
	 * 
	 * 挽留
	 * @param str $key
	 */
	public  function urge($key) {
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( empty($playerInfo['boyfriend_id']) ) {
			return array('code'=>223,'ret'=>'');
		}
		//男朋友信息
		$boyfriendId = $playerInfo['boyfriend_id'];
		$boyfriendInfo = $this->cache->get("boyfriend_".$boyfriendId);
		if ( empty($boyfriendInfo) ) {
			return array('code'=>2,'ret'=>'');
		}
		if ( $boyfriendInfo['mood'] > 0 ) {
			return array('code'=>228,'ret'=>'');
		}
		//判断元宝是否足够
		$pay = 20;
		if ( $pay > $playerInfo['cash'] ) {
			return array('code'=>10,'ret'=>'');
		}
		//更新男朋友信息
		$updateData = array();
		$boyfriendInfo['mood'] = 100; //增加100点好感度
		$updateData['mood'] = $boyfriendInfo['mood'];
		$boyfriendInfo['update_time'] = time();
		$updateData['update_time'] = $boyfriendInfo['update_time'];
		
		$options = array(
			'key'=>array(
				'id'=>$boyfriendId,
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_boyfriend', $options);
		$this->cache->set("boyfriend_".$boyfriendId, $boyfriendInfo);
		//更新用户信息
		$updateData = array();
		$updateData['cash'] = $playerInfo['cash'] - $pay;
		$playerInfo['cash'] = $updateData['cash'];
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$this->cache->set("ku_".$key, $playerInfo);
		//写元宝log
		if ( $pay > 0 ) {
			$aryData = array(
				'boy_id'=>$boyfriendId,
			);
			$options = array(
				'u_id'=> $playerInfo['u_id'],
				'type'=> 'urge',
				'num'=> -$pay,
				'data'=> serialize($aryData),
				'add_time'=> time(),
			);
			Common::cashLog($options);
		}
		
		$ret = array(
			'mood'=>$boyfriendInfo['mood'],
			'cash'=>$playerInfo['cash'],
			'get_mood'=>100,
		);
		return array('code'=>0,'ret'=>$ret);
	}
	
	/**
	 * 
	 * 分手
	 * @param str $key
	 */
	public function breakup($key) {
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( empty($playerInfo['boyfriend_id']) ) {
			return array('code'=>223,'ret'=>'');
		}
		//男朋友信息
		$boyfriendId = $playerInfo['boyfriend_id'];
		$boyfriendInfo = $this->cache->get("boyfriend_".$boyfriendId);
		if ( empty($boyfriendInfo) ) {
			return array('code'=>2,'ret'=>'');
		}
		//移除buff
		$itemModel = new ItemModel();
		$itemModel->removeBuff($boyfriendInfo['bonus'], $playerInfo);
		//清理男朋友相关信息
		$this->db->query("delete from player_boyfriend where `id`={$boyfriendId} limit 1");
		$this->db->query("delete from player_buff where `u_id`={$playerInfo['u_id']} and `item_label`='0' limit 1");
		$this->db->query("update player_info set `boyfriend_id`=0 where `u_id`={$playerInfo['u_id']} limit 1");
		$this->cache->delete('boyfriend_'.$boyfriendId);
		$playerInfo['boyfriend_id'] = 0;
		$this->cache->set("ku_".$key, $playerInfo);
		$countDown = 0;
		if ( $playerInfo['now_energy'] < $playerInfo['max_energy'] ) {
			$countDown = $playerInfo['next_energy_time'] - time();
		}
		//返回个人信息
		$ret = array(
			'now_energy'=>$playerInfo['now_energy'],
			'max_energy'=>$playerInfo['max_energy'],
			'hanger'=>$playerInfo['hanger'],
			'count_down'=>$countDown,
			'coin_factor'=>$playerInfo['coin_factor'],
			'hanger_factor'=>$playerInfo['hanger_factor'],
			'cd_factor'=>$playerInfo['cd_factor'],
		);
		return array('code'=>0,'ret'=>$ret);
	}
	
	/**
	 * 
	 * 约会
	 * @param str $key
	 * @param str $type dinner、movie、travel
	 */
	public function dating($key, $type) { //约会
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( empty($playerInfo['boyfriend_id']) ) {
			return array('code'=>223,'ret'=>'');
		}
		//男朋友信息
		$boyfriendId = $playerInfo['boyfriend_id'];
		$boyfriendInfo = $this->cache->get("boyfriend_".$boyfriendId);
		if ( empty($boyfriendInfo) ) {
			return array('code'=>2,'ret'=>'');
		}
		if( !isset($this->boyFriendDate[$type]) ) {
			return array('code'=>229,'ret'=>''); 
		}
		if ( $this->boyFriendDate[$type]['club_hotness'] > $playerInfo['club_hotness'] ) {
			return array('code'=>212,'ret'=>''); 
		}
		//判断钱
		if ( isset($this->boyFriendDate[$type]['coin']) ) {
			if ( $this->boyFriendDate[$type]['coin'] > $playerInfo['coin'] ) {
				return array('code'=>9,'ret'=>''); 
			}
		}
		if ( isset($this->boyFriendDate[$type]['cash']) ) {
			if ( $this->boyFriendDate[$type]['cash'] > $playerInfo['cash'] ) {
				return array('code'=>10,'ret'=>''); 
			}
		}
		//更新男朋友信息
		$updateData = array();
		$boyfriendInfo['mood'] += $this->boyFriendDate[$type]['mood']; //增加好感度
		$boyfriendInfo['mood'] = $boyfriendInfo['mood'] > 100 ? 100 : $boyfriendInfo['mood'];
		$updateData['mood'] = $boyfriendInfo['mood'];
		$options = array(
			'key'=>array(
				'id'=>$boyfriendId,
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_boyfriend', $options);
		$this->cache->set("boyfriend_".$boyfriendId, $boyfriendInfo);
		//更新用户信息
		$updateData = array();
		if ( isset($this->boyFriendDate[$type]['cash']) ) { 
			$updateData['cash'] = $playerInfo['cash'] - $this->boyFriendDate[$type]['cash'];
			$playerInfo['cash'] = $updateData['cash'];
		}
		if ( isset($this->boyFriendDate[$type]['coin']) ) { 
			$updateData['coin'] = $playerInfo['coin'] - $this->boyFriendDate[$type]['coin'];
			$playerInfo['coin'] = $updateData['coin'];
		}
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$this->cache->set("ku_".$key, $playerInfo);
		//写元宝log
		if ( isset($this->boyFriendDate[$type]['cash']) ) {
			$aryData = array(
				'boy_id'=>$boyfriendId,
				'type'=>$type,
			);
			$options = array(
				'u_id'=> $playerInfo['u_id'],
				'type'=> 'dating',
				'num'=> -$this->boyFriendDate[$type]['cash'],
				'data'=> serialize($aryData),
				'add_time'=> time(),
			);
			Common::cashLog($options);
		}
		$ret = array(
			'mood'=>$boyfriendInfo['mood'],
			'cash'=>$playerInfo['cash'],
			'coin'=>$playerInfo['coin'],
			'get_mood'=>$this->boyFriendDate[$type]['mood'],
		);
		return array('code'=>0,'ret'=>$ret);
	}
	
	/**
	 * 
	 * 获取可送男朋友的礼物列表
	 * @param str $key 
	 * @param str $shopId 商店标识
	 */
	public function getGiftList($key, $shopId) {
		$itemModel = new ItemModel();
		return $itemModel->getItem($key, $shopId);
	}
	
	/**
	 * 
	 * 买礼物给男朋友
	 * @param str $key
	 * @param str $label
	 */
	public function buyGift($key, $label) {
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if ($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( empty($playerInfo['boyfriend_id']) ) {
			return array('code'=>223,'ret'=>'');
		}
		//男朋友信息
		$boyfriendId = $playerInfo['boyfriend_id'];
		$boyfriendInfo = $this->cache->get("boyfriend_".$boyfriendId);
		
		if ( empty($boyfriendInfo) ) {
			return array('code'=>2,'ret'=>'');
		}
		
		if ( !empty($label) ) {
			$itemData = $this->cache->get('itemdata_'.$label);
			if ( empty($itemData) ) {
				$query = $this->db->query("select * from item where `label`='{$label}'");
				$itemData = $this->db->fetchOneByQuery($query);
				$this->cache->set('itemdata_'.$label, $itemData);
			}
			if ( empty($itemData) ) { //不存在该道具
				return array('code'=>213,'ret'=>'');
			}
		} else {
			return array('code'=>213,'ret'=>'');
		}
		
		if ( $itemData['type']!=2 ) {
			return array('code'=>214,'ret'=>'');
		}
		//判断钱是否足够
		if ( $itemData['cost_type'] == 1 ) {
			if ( $itemData['price'] > $playerInfo['coin'] ) {
				return array('code'=>9,'ret'=>'');
			}
		} else {
			if ( $itemData['price'] > $playerInfo['cash'] ) {
				return array('code'=>10,'ret'=>'');
			}
		}
		
		//判断叠加限制 预留
		$updateData = array();
		$boyfriendInfo['favor'] += $itemData['bonus']; //增加好感度
		$isGetItem = $this->getGiftbyFavor($boyfriendInfo['favor'], $itemData['bonus'], $playerInfo);
		$updateData['favor'] = $boyfriendInfo['favor'];
		//更新信息
		$options = array(
			'key'=>array(
				'id'=>$boyfriendId,
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_boyfriend', $options);
		$this->cache->set("boyfriend_".$boyfriendId, $boyfriendInfo);
		
		$updateData = array();
		if ( $itemData['cost_type'] == 1 ) {
			$updateData['coin'] = $playerInfo['coin'] - $itemData['price'];
		} else {
			$updateData['cash'] = $playerInfo['cash'] - $itemData['price'];
		}
		$playerInfo = array_merge($playerInfo, $updateData);
		//更新角色信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$this->cache->set("ku_".$key, $playerInfo);
		//写元宝log
		if ( isset($updateData['cash']) ) {
			$aryData = array(
				'label'=>$label,
				'boyfriend_label'=>$boyfriendInfo['boyfriend'],
				'boyfriend_id'=>$boyfriendId,
			);
			$options = array(
				'u_id'=> $playerInfo['u_id'],
				'type'=> 'gift4boy',
				'num'=> -$itemData['price'],
				'data'=> serialize($aryData),
				'add_time'=> time(),
			);
			Common::cashLog($options);
		}
		//返回个人信息
		$ret = array(
			'favor'=>$boyfriendInfo['favor'],
			'coin'=>$playerInfo['coin'],
			'cash'=>$playerInfo['cash'],
			'hanger'=>$playerInfo['hanger'],
			'gift'=>$isGetItem===false ? '' : $isGetItem,
			'get_favor'=>$itemData['bonus'],
		);
		return array('code'=>0,'ret'=>$ret);
	}
	/**
	 * 
	 * 判断好心值是否足够获得礼物
	 * @param int $favor 现在的好感度
	 * @param int $addFavor 增加的好感度
	 * @param array $playerInfo 角色信息
	 */
	private function getGiftbyFavor($favor, $addFavor, &$playerInfo) {
		$oldFavor = $favor - $addFavor;
		$oldLev =  floor($oldFavor/10);
		$newLev =  floor($favor/10);
		if( $newLev <= 3 && $oldLev!=$newLev ) {
			$getItem = $this->getGiftLabel($playerInfo['acc_key'], $boyfriend['quality']);
			if ( $getItem!==false ){
				return false;
			}
			$insertData = array(
				'u_id'=>$playerInfo['u_id'],
				'label'=>$getItem,
				'is_now'=>0,
			);
			$this->db->insertData('player_clothes', $insertData);
			$userinfoModel = new UserinfoModel();
			$playerClothes = $userinfoModel->getAllClothes($playerInfo['acc_key']);
			$clothesData = $this->cache->get('clothes_'.$getItem);
			if ( empty($clothesData) ) {
				$query = $this->db->query("select * from clothes where `label`='{$getItem}'");
				$clothesData = $this->db->fetchOneByQuery($query);
				$this->cache->set('clothes_'.$getItem, $clothesData);
			}
			if ( empty($clothesData) ) { //不存在该衣服
				return false;
			}
			$playerClothes[$getItem] = $clothesData['hotness'];
			$this->cache->set('my_clothes_'.$playerInfo['u_id'], $playerClothes);
			//更新角色信息
			$updateData = array();
			$updateData['hanger'] = $playerInfo['hanger'] + 1;
			$playerInfo['hanger'] = $updateData['hanger'];
			$options = array(
				'key'=>array(
					'u_id'=>$playerInfo['u_id'],
				),
				'data'=>$updateData,
			);
			$this->db->updateData('player_info', $options);
			$this->cache->set("ku_".$playerInfo['acc_key'], $playerInfo);
			return $getItem; //返回道具标识
		}
		return false;
	}
	
	public function getGiftLabel($key, $quality) { //获取礼物标识
		$userinfoModel = new UserinfoModel();
		$allClothes = $userinfoModel->getAllClothes($key);
		global $itemFromBoyfriend;
		$aryQuality = array(1, 2);
		$quality = in_array($quality, $aryQuality) ? $quality : 1;
		$aryItem = $itemFromBoyfriend[$quality];
		$diffItem = array_diff($aryItem, $allClothes);
		if ( count($diffItem) == 0 ) {
			return 'item_1';
		} else {
			$randKeys = array_rand($diffItem, 1);
			$label = $diffItem[$randKeys[0]];
			return $label;
		}
	}
}
?>